Imports System.IO

Namespace Threads
    ''' <summary>
    ''' This is a base class to define a Template for Bikirom threads. This base class will be extended for all other tasks. This thread does not complete the threading process
    ''' </summary>
    ''' <remarks></remarks>
    Friend Class BikiThread

        '' ------------------------------ Standard variables to be passed back
        Protected Friend m_progress As Integer
        Protected Friend m_message As String

        '' ------------------------------ Standard Events to pass back
        Friend Event Progress(ByVal sender As Object, ByVal e As BikiThread_Args)
        Friend Event Message(ByVal sender As Object, ByVal e As BikiThread_Args)

        Protected Friend bgw As System.ComponentModel.BackgroundWorker
        Protected Friend eventArgs As System.ComponentModel.DoWorkEventArgs


        Protected Friend Sub OnProgress(ByVal progress As Integer)
            m_progress = progress
            RaiseEvent Progress(Me, New BikiThread_Args(m_progress, "", Nothing))
        End Sub
        Protected Friend Sub OnMessage(ByVal Message As String)
            m_message = Message
            RaiseEvent Message(Me, New BikiThread_Args(-1, m_message, Nothing))
        End Sub
        Protected Friend Sub OnObject(ByVal [Object] As Object)
            RaiseEvent Message(Me, New BikiThread_Args(m_progress, m_message, [Object]))
        End Sub

        ''' <summary>
        ''' Base Execute routine. CALL THIS ALWAYS!
        ''' </summary>
        ''' <param name="backGroundWorker">the background worker object from the main UI thread</param>
        ''' <param name="e"></param>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Friend Function Execute(ByVal backGroundWorker As System.ComponentModel.BackgroundWorker, _
                 ByVal e As System.ComponentModel.DoWorkEventArgs) As Boolean
            bgw = backGroundWorker
            eventArgs = e
            Return True
        End Function
    End Class
    ''' <summary>
    ''' Basic event argument class to pass back infomation to the UI thread
    ''' </summary>
    ''' <remarks></remarks>
    Friend Class BikiThread_Args
        Inherits EventArgs

        Protected Friend m_Progress As Integer
        Protected Friend m_Message As String = ""
        Protected Friend m_object As Object = Nothing

        Friend ReadOnly Property Progress() As Integer
            Get
                Return m_Progress
            End Get
        End Property
        Friend ReadOnly Property Message() As String
            Get
                Return m_Message
            End Get
        End Property
        Friend Sub New(ByVal percent As Integer, ByVal Message As String, ByVal [Object] As Object)
            m_Progress = percent
            m_Message = Message
            m_object = [Object]
        End Sub
        Friend ReadOnly Property [Object]() As Object
            Get
                Return m_object
            End Get
        End Property
    End Class

    Friend Class QueueObj
        Friend m_Object As Object
        Friend m_ProgressForm As frmProgress
        Friend m_Process As threadAction
        Friend m_Action As BikiAction
        Friend m_tries As Short = 0
        Friend ReadOnly Property Process() As String
            Get
                Select Case m_Process
                    Case threadAction.TH_BIK_DOWNLOAD
                        Return "BIKIROM EEPROM DOWNLOAD"
                    Case threadAction.TH_BIK_UPLOAD
                        Return "BIKIROM EEPROM UPLOAD"
                    Case threadAction.TH_DOWN
                        Return "THEAD DOWNLOAD"
                    Case threadAction.TH_UPLOAD
                        Return "THEAD UPLOAD"
                    Case Else
                        Return "SOMTEHING ELSE?"
                End Select
            End Get
        End Property

        Friend Sub New(ByVal TH_Object As Object, ByVal Process As threadAction, ByVal Action As BikiAction, Optional ByVal ProgressForm As frmProgress = Nothing)
            m_Object = TH_Object
            m_Process = Process
            m_Action = Action
            m_ProgressForm = ProgressForm
        End Sub
    End Class
    Friend Class Queue
        Inherits ArrayList

        ''' <summary>
        ''' Remove the first item from the queue and return it
        ''' </summary>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Friend Function Dequeue() As QueueObj
            If Me.Count >= 1 Then
                Dim f As QueueObj = Me(0)
                Me.RemoveAt(0)
                Return f
            Else
                Return Nothing
            End If
        End Function
        ''' <summary>
        ''' Add a Thread Object at the end of the queue
        ''' </summary>
        ''' <param name="qObject"></param>
        ''' <remarks></remarks>
        Friend Sub Enqueue(ByRef qObject As QueueObj)
            Select Case qObject.m_Process
                Case threadAction.TH_EMUALTOR
                    Dim f_oTmp As Emulator = CType(qObject.m_Object, Emulator)
                    If Me.Count >= 1 Then
                        Dim f_tmp As QueueObj
                        For i As Short = 0 To Me.Count - 1
                            f_tmp = CType(Me(i), QueueObj)
                            If f_tmp.m_Process = threadAction.TH_EMUALTOR Then
                                Dim f_qTmp As Emulator = CType(f_tmp.m_Object, Emulator)
                                If f_qTmp = f_oTmp Then
                                    Me.RemoveAt(i)
                                    Exit For
                                End If
                            End If
                        Next
                    End If
            End Select
            Me.Add(qObject)
        End Sub
        Default Public Shadows Property Item(ByVal index As Integer) As QueueObj
            Get
                Return CType(MyBase.Item(index), QueueObj)
            End Get
            Set(ByVal value As QueueObj)
                MyBase.Item(index) = value
            End Set
        End Property
    End Class
End Namespace
